﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using BattleCity.GameLogic;

namespace BattleCity
{
    public class OptionsMenu
    {
        public enum OptionsScreen
        {
            Main,
            Sound,
            Antialiasing,
            EnBullet,
            TanksLights,
            OnOff,
            None
        }

        Vector2[] stringsPositions;
        Vector2 soundTitlePosition;
        Vector2 antialiasingTitlePosition;
        Vector2 enBulletPosition;
        Vector2 tankLightsPosition;
        Vector2 onPosition;
        Vector2 offPosition;

        Vector2 menuWrapperPosition;

        SpriteFont menuFont;
        Texture2D menuWrapper;
        SpriteBatch spriteBatch;

        Texture2D button;
        Texture2D tapped;

        int buttonX;

        public OptionsScreen optionsScreen;

        OptionsScreen selected;
        float tappedCounter;

        public OptionsMenu(Vector2 screenCenter, SpriteFont menuFont, Texture2D menuWrapper, Texture2D button, Texture2D tapped, SpriteBatch spriteBatch)
        {
            stringsPositions = new Vector2[2];

            this.menuWrapper = menuWrapper;
            this.button = button;
            this.tapped = tapped;
            this.menuFont = menuFont;
            this.spriteBatch = spriteBatch;

            stringsPositions = new Vector2[4];
            stringsPositions[0] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.SOUND).X * 0.5f, 50.0f);
            stringsPositions[1] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.TANKLIGHTS).X * 0.5f, 160.0f);
            stringsPositions[2] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.ENHBULLET).X * 0.5f, 270.0f);
            stringsPositions[3] = new Vector2(screenCenter.X - menuFont.MeasureString(strings.ANTIALIASING).X * 0.5f, 380.0f);

            soundTitlePosition = new Vector2(screenCenter.X - menuFont.MeasureString(strings.SOUND).X * 0.5f, 50.0f);
            antialiasingTitlePosition = new Vector2(screenCenter.X - menuFont.MeasureString(strings.ANTIALIASING).X * 0.5f, 50.0f);
            enBulletPosition = new Vector2(screenCenter.X - menuFont.MeasureString(strings.ENHBULLET).X * 0.5f, 50.0f);
            tankLightsPosition = new Vector2(screenCenter.X - menuFont.MeasureString(strings.TANKLIGHTS).X * 0.5f, 50.0f);
            onPosition = new Vector2(screenCenter.X - menuFont.MeasureString(strings.ON).X * 0.5f, 240.0f);
            offPosition = new Vector2(screenCenter.X - menuFont.MeasureString(strings.OFF).X * 0.5f, 240.0f);

            buttonX = (int)screenCenter.X - (button.Width / 2);

            #if WINDOWS_PHONE 
            menuWrapperPosition = new Vector2(screenCenter.X - 200, 0.0f);
            #else
            menuWrapperPosition = new Vector2(screenCenter.X - 300, 0.0f);
            #endif

            optionsScreen = OptionsScreen.Main;
        }

        public void Update(Vector2 gsPosition)
        {
            if (tappedCounter == 0.0f)
            {
                if ((gsPosition.X >= menuWrapperPosition.X) && (gsPosition.X <= menuWrapperPosition.X + menuWrapper.Width))
                {
                    switch (optionsScreen)
                    {
                        case OptionsScreen.Main:
                            if (gsPosition.Y < stringsPositions[0].Y + menuFont.MeasureString(strings.SOUND).Y)
                                selected = OptionsScreen.Sound;
                            else if (gsPosition.Y < stringsPositions[1].Y + menuFont.MeasureString(strings.TANKLIGHTS).Y)
                                selected = OptionsScreen.TanksLights;
                            else if (gsPosition.Y < stringsPositions[2].Y + menuFont.MeasureString(strings.ENHBULLET).Y)
                                selected = OptionsScreen.EnBullet;
                            else if (gsPosition.Y < stringsPositions[3].Y + menuFont.MeasureString(strings.ANTIALIASING).Y)
                                selected = OptionsScreen.Antialiasing;
                            break;

                        case OptionsScreen.Sound:
                            if ((gsPosition.Y >= onPosition.Y) && (gsPosition.Y <= onPosition.Y + menuFont.MeasureString(strings.ON).Y))
                            {
                                Options.soundEnabled = !Options.soundEnabled;
                                selected = OptionsScreen.OnOff;
                            }
                            if (!Options.soundEnabled) Sound.stopMusic();
                            else Sound.startMenuMusic();
                            break;

                        case OptionsScreen.Antialiasing:
                            if ((gsPosition.Y >= onPosition.Y) && (gsPosition.Y <= onPosition.Y + menuFont.MeasureString(strings.ON).Y))
                            {
                                Options.setAntialiasing(!Options.antialiasingEnabled);
                                selected = OptionsScreen.OnOff;
                            }
                            break;

                        case OptionsScreen.EnBullet:
                            if ((gsPosition.Y >= onPosition.Y) && (gsPosition.Y <= onPosition.Y + menuFont.MeasureString(strings.ON).Y))
                            {
                                Options.enhancedBullets = !Options.enhancedBullets;
                                selected = OptionsScreen.OnOff;
                            }
                            break;

                        case OptionsScreen.TanksLights:
                            if ((gsPosition.Y >= onPosition.Y) && (gsPosition.Y <= onPosition.Y + menuFont.MeasureString(strings.ON).Y))
                            {
                                Options.tanksLights = !Options.tanksLights;
                                selected = OptionsScreen.OnOff;
                            }
                            break;
                    }
                }

                if (selected != OptionsScreen.None)
                {
                    if (Options.soundEnabled) Sound.playSound(ref Sound.buttonPressed);
                    tappedCounter = 0.2f;
                }
            }
        }

        public void UpdateCounter(float dt)
        {
            if (tappedCounter > 0.0f)
            {
                tappedCounter -= dt;

            }

            if ((tappedCounter <= 0.0f) && (selected != OptionsScreen.None))
            {
                tappedCounter = 0.0f;
                if(selected != OptionsScreen.OnOff)   optionsScreen = selected;
                selected = OptionsScreen.None;
            }
        }

        public void Draw()
        {
            spriteBatch.Draw(menuWrapper, menuWrapperPosition, Color.White);

            switch (optionsScreen)
            {
                case OptionsScreen.Main:
                     spriteBatch.Draw(getButton(OptionsScreen.Sound), new Vector2(buttonX, stringsPositions[0].Y - 20), Color.White);
                     spriteBatch.Draw(getButton(OptionsScreen.TanksLights), new Vector2(buttonX, stringsPositions[1].Y - 20), Color.White);
                     spriteBatch.Draw(getButton(OptionsScreen.EnBullet), new Vector2(buttonX, stringsPositions[2].Y - 20), Color.White);
                     spriteBatch.Draw(getButton(OptionsScreen.Antialiasing), new Vector2(buttonX, stringsPositions[3].Y - 20), Color.White);

                     spriteBatch.DrawString(menuFont, strings.SOUND, stringsPositions[0] + new Vector2(2, 2), Color.Black);
                     spriteBatch.DrawString(menuFont, strings.TANKLIGHTS, stringsPositions[1] + new Vector2(2, 2), Color.Black);
                     spriteBatch.DrawString(menuFont, strings.ENHBULLET, stringsPositions[2] + new Vector2(2, 2), Color.Black);
                     spriteBatch.DrawString(menuFont, strings.ANTIALIASING, stringsPositions[3] + new Vector2(2, 2), Color.Black);

                     spriteBatch.DrawString(menuFont, strings.SOUND, stringsPositions[0], Color.DarkCyan);
                     spriteBatch.DrawString(menuFont, strings.TANKLIGHTS, stringsPositions[1], Color.DarkCyan);
                     spriteBatch.DrawString(menuFont, strings.ENHBULLET, stringsPositions[2], Color.DarkCyan);
                     spriteBatch.DrawString(menuFont, strings.ANTIALIASING, stringsPositions[3], Color.DarkCyan);
                    break;

                case OptionsScreen.Sound:
                    spriteBatch.DrawString(menuFont, strings.SOUND, soundTitlePosition, Color.Green);
                    spriteBatch.Draw(getButton(OptionsScreen.OnOff), new Vector2(buttonX, onPosition.Y - 20), Color.White);
                    if (Options.soundEnabled)
                    {
                        spriteBatch.DrawString(menuFont, strings.ON, onPosition + new Vector2(2, 2), Color.Black);
                        spriteBatch.DrawString(menuFont, strings.ON, onPosition, Color.DarkCyan);
                    }
                    else
                    {
                        spriteBatch.DrawString(menuFont, strings.OFF, offPosition + new Vector2(2, 2), Color.Black);
                        spriteBatch.DrawString(menuFont, strings.OFF, offPosition, Color.DarkCyan);
                    }
                    break;

                case OptionsScreen.Antialiasing:
                    spriteBatch.DrawString(menuFont, strings.ANTIALIASING, antialiasingTitlePosition, Color.Green);
                    spriteBatch.Draw(getButton(OptionsScreen.OnOff), new Vector2(buttonX, onPosition.Y - 20), Color.White);
                    if (Options.antialiasingEnabled)
                    {
                        spriteBatch.DrawString(menuFont, strings.ON, onPosition + new Vector2(2, 2), Color.Black);
                        spriteBatch.DrawString(menuFont, strings.ON, onPosition, Color.DarkCyan);
                    }
                    else
                    {
                        spriteBatch.DrawString(menuFont, strings.OFF, offPosition + new Vector2(2, 2), Color.Black);
                        spriteBatch.DrawString(menuFont, strings.OFF, offPosition, Color.DarkCyan);
                    }
                    break;

                case OptionsScreen.TanksLights:
                    spriteBatch.DrawString(menuFont, strings.TANKLIGHTS, tankLightsPosition, Color.Green);
                    spriteBatch.Draw(getButton(OptionsScreen.OnOff), new Vector2(buttonX, onPosition.Y - 20), Color.White);
                    if (Options.tanksLights)
                    {
                        spriteBatch.DrawString(menuFont, strings.ON, onPosition + new Vector2(2, 2), Color.Black);
                        spriteBatch.DrawString(menuFont, strings.ON, onPosition, Color.DarkCyan);
                    }
                    else
                    {
                        spriteBatch.DrawString(menuFont, strings.OFF, offPosition + new Vector2(2, 2), Color.Black);
                        spriteBatch.DrawString(menuFont, strings.OFF, offPosition, Color.DarkCyan);
                    }
                    break;

                case OptionsScreen.EnBullet:
                    spriteBatch.DrawString(menuFont, strings.ENHBULLET, enBulletPosition, Color.Green);
                    spriteBatch.Draw(getButton(OptionsScreen.OnOff), new Vector2(buttonX, onPosition.Y - 20), Color.White);
                    if (Options.enhancedBullets)
                    {
                        spriteBatch.DrawString(menuFont, strings.ON, onPosition + new Vector2(2, 2), Color.Black);
                        spriteBatch.DrawString(menuFont, strings.ON, onPosition, Color.DarkCyan);
                    }
                    else
                    {
                        spriteBatch.DrawString(menuFont, strings.OFF, offPosition + new Vector2(2, 2), Color.Black);
                        spriteBatch.DrawString(menuFont, strings.OFF, offPosition, Color.DarkCyan);
                    }
                    break;
            }
           
            
        }

        Texture2D getButton(OptionsScreen buttonPressed)
        {
            if (selected == buttonPressed) return tapped;
            else return button;
        }
    }
}
